home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmiSoft / Comm / tcp / JabberwockySRC.lha / Jabberwocky / src / agents.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-04-28  |  8.0 KB  |  279 lines

  1. /*  Copyright (C) 2002 Tom Parker (tom@carrott.org),
  2.                        Matthias Münch (matthias@amigaworld.de)
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17.  
  18.     In addition, as a special exception, Tom Parker and Matthias Münch give
  19.     permission to link the code of this program with a TCP stack of your 
  20.     choice, any official MUI libraries or classes and any custom MUI classes
  21.     that should be necessary for the operation of this program.  This 
  22.     exception also gives you permission to distribute linked combinations 
  23.     including this software with any of the before-mentioned libraries and 
  24.     classes.  You must obey the GNU General Public License in all respects for
  25.     all of the code used other than that provided by the before-mentioned 
  26.     libraries and classes.  As part of this exception you are obliged to 
  27.     follow the license terms of the before-mentioned libraries, this license
  28.     does not compel you to follow those terms, but if you do not then you may
  29.     not link with those libraries.  If you modify this file, you may extend
  30.     this exception to your version of the file, but you are not obligated to
  31.     do so.  If you do not wish to do so, delete this exception statement from
  32.     your version.
  33. */
  34. /*
  35. ** agent management
  36. */
  37.  
  38. #include "common.h"
  39.  
  40. #include <MUI/NListview_mcc.h>
  41. #include <MUI/NFloattext_mcc.h>
  42.  
  43. static ULONG agents_new(struct IClass *cl, Object *obj, struct opSet *msg);
  44. static MUI_LIST_DISP_DECL(listdisp, jagent a);
  45. static void agents_parse(struct agentsdata *data, ikspak *pak);
  46. static void agents_updateinfo(struct agentsdata *data);
  47. static void agents_register(struct agentsdata *data);
  48. static void agents_search(struct agentsdata *data);
  49.  
  50.  
  51. MUI_DISPATCH(agents_dispatch)
  52. {
  53.     switch(msg->MethodID)
  54.     {
  55.     case OM_NEW:
  56.         return agents_new(cl,obj,(APTR)msg);
  57.  
  58.     case AGENTS_PARSE:
  59.         agents_parse(INST_DATA(cl,obj), (ikspak *)MARG1);
  60.         return 0;
  61.  
  62.     case AGENTS_UPDATEINFO:
  63.         agents_updateinfo(INST_DATA(cl,obj));
  64.         return 0;
  65.  
  66.     case AGENTS_REG:
  67.         agents_register(INST_DATA(cl,obj));
  68.         return 0;
  69.  
  70.     case AGENTS_SEARCH:
  71.         agents_search(INST_DATA(cl,obj));
  72.         return 0;
  73.  
  74.     }
  75.     return DoSuperMethodA(cl, obj, msg);
  76. }
  77.  
  78.  
  79. static ULONG agents_new(struct IClass *cl, Object *obj, struct opSet *msg)
  80. {
  81.     static struct Hook dispHook = { {0,0}, &listdisp, NULL, NULL };
  82.     struct agentsdata *data;
  83.     Object *list, *url, *desc, *regbut, *editbut, *searchbut, *urlbut;
  84.  
  85.     obj = (Object *)DoSuperNew(cl,obj,
  86.         MUIA_Window_ID, MAKE_ID('A', 'G', 'E', 'N'),
  87.         MUIA_Window_Title, MSG_AGENTS_TITLE,
  88.         MUIA_HelpNode, "window-agents",
  89.         WindowContents, VGroup,
  90.             Child, NListviewObject,
  91.                 MUIA_NListview_NList, (ULONG) list = NListObject,
  92.                     InputListFrame,
  93.                     MUIA_NList_Title, TRUE,
  94.                     MUIA_NList_Format, "BAR, BAR, BAR, BAR",
  95.                     MUIA_NList_DisplayHook, &dispHook,
  96.                     MUIA_CycleChain, 1,
  97.                 End,
  98.             End,
  99.             Child, VGroup,
  100.                 MUIA_Group_Columns, 2,
  101.                 Child, Label1(MSG_AGENTS_URL),
  102.                 Child, HGroup,
  103.                     Child, (ULONG) url = TextObject,
  104.                         TextFrame,
  105.                         MUIA_Background, MUII_TextBack,
  106.                     End,
  107.                     Child, urlbut = mui_button(MSG_AGENTS_VISIT_GAD),
  108.                 End,
  109.                 Child, VGroup,
  110.                     MUIA_HorizWeight, 0,
  111.                     Child, Label1(MSG_AGENTS_DESCRIPTION),
  112.                     Child, RectangleObject, End,
  113.                 End,
  114.                 Child, NListviewObject,
  115.                     MUIA_FixHeightTxt, "\n\n\n",
  116.                     MUIA_NListview_NList, (ULONG) desc = NFloattextObject,
  117.                         ReadListFrame,
  118.                     End,
  119.                 End,
  120.             End,
  121.             Child, HGroup,
  122.                 Child, regbut = mui_button(MSG_AGENTS_REGISTER_GAD),
  123.                 Child, searchbut = mui_button(MSG_AGENTS_SEARCH_GAD),
  124.                 Child, editbut = mui_button(MSG_AGENTS_CONFERENCE_GAD),
  125.             End,
  126.         End,
  127.         TAG_MORE, msg->ops_AttrList);
  128.  
  129.     if(!obj) return(0);
  130.  
  131.     data = INST_DATA(cl,obj);
  132.     data->list = list;
  133.     data->url = url;
  134.     data->desc = desc;
  135.     data->regbut = regbut;
  136.     data->searchbut = searchbut;
  137.     data->urlbut = urlbut;
  138.  
  139.     set(urlbut, MUIA_HorizWeight, 0);
  140.  
  141.     DoMethod(list, MUIM_Notify, MUIA_NList_EntryClick, MUIV_EveryTime, obj, 1, AGENTS_UPDATEINFO);
  142.     DoMethod(regbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, AGENTS_REG);
  143.     DoMethod(editbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, AGENTS_REG);
  144.     DoMethod(searchbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, AGENTS_SEARCH);
  145.  
  146.  
  147.     DoMethod(obj, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, obj, 3, MUIM_Set, MUIA_Window_Open, FALSE);
  148.  
  149.     return (ULONG)obj;
  150. }
  151.  
  152.  
  153. MUI_LIST_DISP_STATIC(listdisp, jagent a)
  154. {
  155.     if(a) {
  156.         *array++ = a->name;
  157.         if(a->reg) *array++ = MSG_AGENTS_YES; else *array++ = MSG_AGENTS_NO;
  158.         if(a->search) *array++ = MSG_AGENTS_YES; else *array++ = MSG_AGENTS_NO;
  159.         if(a->chat) *array = MSG_AGENTS_YES; else *array = MSG_AGENTS_NO;
  160.     } else {
  161.         *array++ = MSG_AGENTS_COL_NAME;
  162.         *array++ = MSG_AGENTS_COL_REGISTERED;
  163.         *array++ = MSG_AGENTS_COL_SEARCH;
  164.         *array = MSG_AGENTS_COL_CONFERENCE;
  165.     }
  166.  
  167.     return 0;
  168. }
  169.  
  170.  
  171. void agents_fetch(void)
  172. {
  173.     iks *x;
  174.  
  175.     x = iks_make_iq(IKS_TYPE_GET, IKS_NS_AGENTS);
  176.     iks_send(net.parser, x);
  177.     iks_delete(x);
  178. }
  179.  
  180.  
  181. static void agents_parse(struct agentsdata *data, ikspak *pak)
  182. {
  183.     ikspool *p;
  184.     jagent a;
  185.     iks *x;
  186.     
  187.     // we only get the agent list when we log into the server, so clear any
  188.     // agents from a previous login first.
  189.     DoMethod(data->list,MUIM_NList_Clear);
  190.     x = iks_child(iks_find(pak->x, "query"));
  191.     while(x)
  192.     {
  193.         if(iks_type(x) == IKS_TAG)
  194.         {
  195.             p = iks_pool_new(512);
  196.             if(!p) break;
  197.             a = iks_pool_alloc(p, sizeof(jagent_struct));
  198.             if(!a) break;
  199.             memset(a, 0, sizeof(jagent_struct));
  200.  
  201.             a->jid = iks_pool_strdup(p, iks_find_attrib(x, "jid"));
  202.             a->name = iks_pool_strdup(p, iks_find_cdata(x, "name"));
  203.             a->url = iks_pool_strdup(p, iks_find_cdata(x, "url"));
  204.             a->desc = iks_pool_strdup(p, iks_find_cdata(x, "description"));
  205.             a->transport = iks_pool_strdup(p, iks_find_cdata(x, "transport"));
  206.             a->service = iks_pool_strdup(p, iks_find_cdata(x, "service"));
  207.  
  208.             if(iks_find(x, "register")) a->reg=1;
  209.             if(iks_find(x, "search")) a->search=1;
  210.             if(iks_find(x, "groupchat")) a->chat=1;
  211.  
  212.             DoMethod(data->list, MUIM_NList_InsertSingle, a, MUIV_NList_Insert_Bottom);
  213.         }
  214.  
  215.         x = iks_next(x);
  216.     }
  217.  
  218. }
  219.  
  220.  
  221. static void agents_updateinfo(struct agentsdata *data)
  222. {
  223.     u_long t;
  224.     jagent a;
  225.  
  226.     GetAttr(MUIA_NList_EntryClick, data->list, &t);
  227.     if(t == -1 || t == -2) return;
  228.     DoMethod(data->list, MUIM_NList_GetEntry, t, &a);
  229.  
  230.     set(data->url, MUIA_Text_Contents, (ULONG) a->url);
  231.     set(data->desc, MUIA_NFloattext_Text, (ULONG) a->desc);
  232.  
  233.     if(a->reg) set(data->regbut, MUIA_Disabled, FALSE); else set(data->regbut, MUIA_Disabled, TRUE);
  234.     if(a->search) set(data->searchbut, MUIA_Disabled, FALSE); else set(data->searchbut, MUIA_Disabled, TRUE);
  235.     if(a->url) set(data->urlbut, MUIA_Disabled, FALSE); else set(data->urlbut, MUIA_Disabled, TRUE);
  236. }
  237.  
  238.  
  239. static void agents_register(struct agentsdata *data)
  240. {
  241.     u_long t;
  242.     jagent a;
  243.     iks *x;
  244.  
  245.     if(net.state != NET_ON) return;
  246.  
  247.     GetAttr(MUIA_NList_EntryClick, data->list, &t);
  248.     if(t == -1 || t == -2) return;
  249.     DoMethod(data->list, MUIM_NList_GetEntry, t, &a);
  250.  
  251.     if(!a->jid) return;
  252.  
  253.     x = iks_make_iq(IKS_TYPE_GET, IKS_NS_REGISTER);
  254.     iks_insert_attrib(x, "to", a->jid);
  255.     iks_send(net.parser, x);
  256.     iks_delete(x);
  257. }
  258.  
  259.  
  260. static void agents_search(struct agentsdata *data)
  261. {
  262.     u_long t;
  263.     jagent a;
  264.     iks *x;
  265.  
  266.     if(net.state != NET_ON) return;
  267.  
  268.     GetAttr(MUIA_NList_EntryClick, data->list, &t);
  269.     if(t == -1 || t == -2) return;
  270.     DoMethod(data->list, MUIM_NList_GetEntry, t, &a);
  271.  
  272.     if(!a->jid) return;
  273.  
  274.     x = iks_make_iq(IKS_TYPE_GET, IKS_NS_SEARCH);
  275.     iks_insert_attrib(x, "to", a->jid);
  276.     iks_send(net.parser, x);
  277.     iks_delete(x);
  278. }
  279.